import tensorflow as tf

W = tf.Variable([[1, 2, 3], [4, 5, 6]], dtype=tf.float32);
X = tf.Variable([[7, 8], [9, 10], [11, 12]], dtype=tf.float32);

Z = tf.Variable([[7, 8], [9, 10]], dtype=tf.float32);

mmul = tf.matmul(W, X)

d_mmul_W = tf.gradients(mmul, W)
d_mmul_X = tf.gradients(mmul, X)

mmul_Z = tf.matmul(Z, Z)
d_mmul_Z = tf.gradients(mmul_Z, Z)

d2_mmul_Z = tf.gradients(d_mmul_Z, Z)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    
    print('------mmul------')
    print(sess.run(mmul))
    print('------d_mmul_W------')
    print(sess.run(d_mmul_W))
    print('------d_mmul_X------')
    print(sess.run(d_mmul_X))
    
    print('------mmul_Z------')
    print(sess.run(mmul_Z))
    print('------d_mmul_Z------')
    print(sess.run(d_mmul_Z))
    
    print('------d2_mmul_Z------')
    print(sess.run(d2_mmul_Z))